<?php
require_once 'IController.class.php';

abstract class AbstractController implements IController {

    private $model;
    private $service;
    private $usuarioLogado;

    public function getModel() {
        return $this->model;
    }

    public function setModel($model) {
        $this->model = $model;
    }

    public function getService() {
        return $this->service;
    }

    public function setService($service) {
        $this->service = $service;
    }

    public function getUsuarioLogado() {
        return $this->usuarioLogado;
    }

    public function setUsuarioLogado($usuarioLogado) {
        $this->usuarioLogado = $usuarioLogado;
    }

    /**
     * Start here some process.
     * @param $_GET, $_POST or $_REQUEST $objRequest
     */
    protected function init($objRequest) {
        //Nothing here
        //Implement in your Controller
    }

    /**
     * (non-PHPdoc)
     * @see IController::end()
     */
    protected function end($objRequest) {
        //Nothing here
        //Implement in your Controller
    }

    /**
     * This method is implemented in AbstractController.
     * It is calling the other 3 methods.
     * If you want to subscribe, don't forget it.
     * init($objRequest);
     * specificExecute($objRequest);
     * end($objRequest);
     * @param $_GET, $_POST or $_REQUEST $objRequest
     */
    public final function execute($objRequest, $verificarLogin = true) {
        //Criando os objetos necessários para a execucao do fluxo.
        $this->populandoAtributos($objRequest);
        
        //Estar logado nao e obrigatorio no login e em outras telas.
        if ($verificarLogin) {
            if(Util::isUserLogged($_SESSION)) {
                if($this->isModelValido()) {
                    //Setando o usuário logado para facil recuperacao e utilizacao no fluxo de processamento
                    $this->setUsuarioLogado($_SESSION[USUARIO_LOGADO]);
                    $this->executarFluxo($objRequest);
                }
            } else {
                $this->executarFluxoUsuarioNaoLogado();
            }
        } else {
            if($this->isModelValido()) {
                $this->executarFluxo($objRequest);
            } else {
                $this->showResult();
            }
        }
    }
    
    /**
     * Fluxo normal de chamadas de método.
     * @param $_GET, $_POST or $_REQUEST $objRequest
     */
    private function executarFluxo($objRequest) {
        $this->init($objRequest);
        $this->specificExecute($objRequest);
        $this->end($objRequest);
        $this->showResult();
    }
    
    /**
     * Fluxo usuario nao logado.
     * @param $_GET, $_POST or $_REQUEST $objRequest
     */
    private function executarFluxoUsuarioNaoLogado() {
        //Populando o obj result para gerar o json com a informacao de usuario nao logado
        $this->getModel()->setResult(ResultFactory::newResultUsuarioNaoLogado());
        //Exibindo o resultado.
        $this->showResult();
    }

    /**
     * This method show the result on the screen.
     */
    public function showResult() {
        $this->getModel()->getResult()->showResult();
    }

}

?>
